﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DB_connection
{
    class Program
    {
        static void Main(string[] args)
        {
            List<DatabaseConnection> connections = new List<DatabaseConnection> ();
            MySqlConnection mySql = new MySqlConnection("111", new TimeSpan(1000));
            MsSqlConnection msSql = new MsSqlConnection("222", new TimeSpan(1000));

            connections.Add(mySql);
            connections.Add(msSql);
            
            foreach (var connType in connections)
            {
                connType.OpenConnection();
                connType.CloseConnection();

            }
            Console.ReadLine();
        }
    }

    abstract class DatabaseConnection
        {
        private string ConnectionString;
        private TimeSpan Timeout { get; set; }

        public DatabaseConnection(string ConnectionString, TimeSpan Timeout)
        {
            this.ConnectionString = ConnectionString;
            this.Timeout = Timeout;
        }

        public string GetConnectionString
        {
            get
            {
                return ConnectionString;
            }

            set
            {
                if (String.IsNullOrWhiteSpace(value))             
                    throw new InvalidOperationException("ConnectionString can't be null");                          
                    ConnectionString = value;
            }
        }

        public abstract void OpenConnection();

        public abstract void CloseConnection();
        


        }


    class MySqlConnection : DatabaseConnection
    {
        public MySqlConnection(string ConnectionString, TimeSpan Timeout) : base(ConnectionString, Timeout)
        {
        }

        public override void OpenConnection()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Connection to MySql is open");
            Console.ForegroundColor = ConsoleColor.Red;
        }

        public override void CloseConnection()
        {
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Connection to MySql is closed");
            Console.ForegroundColor = ConsoleColor.Red;

        }

    }

        class MsSqlConnection : DatabaseConnection
    { 
        public MsSqlConnection(string ConnectionString, TimeSpan Timeout) : base(ConnectionString, Timeout)
        {
        }

        public override void OpenConnection()
        {
            Console.ForegroundColor = ConsoleColor.Green;    
            Console.WriteLine("Connection to MsSql is open");
            Console.ForegroundColor = ConsoleColor.Yellow;

        }

        public override void CloseConnection()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Connection to MsSql is closed");
            Console.ForegroundColor = ConsoleColor.Yellow;

        }
    }

    class DatabaseCommand
    {
        private readonly DatabaseConnection connection;
        private readonly string sqlExpression;

        public DatabaseCommand(string sql, DatabaseConnection dbconnection)
        {
            if (String.IsNullOrWhiteSpace(sql))
                throw new ArgumentException("SQL");
            sqlExpression = sql;

            if (dbconnection == null)
                throw new ArgumentException("SQL");
            sqlExpression = sql;

            if (dbconnection == null)
                throw new NullReferenceException("Database Connection");
            connection = dbconnection;
        }

               public void Execute()
       {
           connection.OpenConnection();
           Console.WriteLine("Execute SQL query: {0}", sqlExpression);
           connection.CloseConnection();
       }

    }
}
